package com.qianfeng.dbsm.admin.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能说明
 *
 * @author 袁俊杰
 * @date 2022-11-10 15:08:46
 */
@Slf4j
public class JWTUtil {
    //定义秘钥
    private static final String SECRET = "serectkey";

    //设置过期时间
    public static final long EXPRIED = 300*1000;

    //设置过期时间
    public static final long LONGTIME = 600*1000;

    //定义用户账号
    public static final String ACCOUNT = "account";

    //定义map，用于存储短token和长token
    static Map<String, String> tokenMap = new HashMap<String, String>();

    public static Map<String, String> getTokenMap() {
        return tokenMap;
    }

    public static void setTokenMap(Map<String, String> tokenMap) {
        JWTUtil.tokenMap = tokenMap;
    }

    //判断tokenmap中是否存在对应token
    public static boolean check(String token){
        return JWTUtil.getTokenMap().containsKey(token);
    }

    //向tokenMap中存放长token和短token
    public static void put(String dstring, String cstring){
        JWTUtil.getTokenMap().put(dstring,cstring);
    }
    //删除无效的tokenmap
    public static void delete(String string){
        JWTUtil.getTokenMap().remove(string);
    }


    //删除用户的用于存储短token和长token的map
    public static void logout(String token){
        JWTUtil.getTokenMap().remove(token);
    }

    //创建第二个token用于验证是否超时
    public static String create2(String account){
        //根据密码创建算法，secret是密钥，可以自定义
        Algorithm algorithm = Algorithm.HMAC256(SECRET);
        String token = JWT.create()
                .withClaim(ACCOUNT,account)
                .withExpiresAt(new Date(System.currentTimeMillis()+LONGTIME))//设置有效时间
                .sign(algorithm);
        return token;
    }

    //实现jwt的创建
    public static String create(String account){
        //根据密码创建算法，secret是密钥，可以自定义
        Algorithm algorithm = Algorithm.HMAC256(SECRET);
        String token = JWT.create()
                .withClaim(ACCOUNT,account)
                .withExpiresAt(new Date(System.currentTimeMillis()+EXPRIED))//设置有效时间
                .sign(algorithm);
        return token;
    }
    //实现jwt的验证
    public static boolean verify(String token){
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            JWTVerifier verifier = JWT.require(algorithm)
                    .build(); //Reusable verifier instance
            DecodedJWT jwt = verifier.verify(token);
            log.info("verify jwt success");
            return true;
        } catch (JWTVerificationException exception){
            exception.printStackTrace();
            throw exception;
        }

    }
    //获取用户信息
    public static String getAccount(String token){
        DecodedJWT jwt = JWT.decode(token);
        //获取自定义参数,获取之前用户的名称
        String account =  jwt.getClaim(ACCOUNT).asString();
        return account;
    }
}
